using System;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Guids;
using CKY.AgentPlatform.Users;

namespace CKY.AgentPlatform.Data
{
    /// <summary>
    /// 默认角色数据种子
    /// </summary>
    public class DefaultRoleDataSeeder : ITransientDependency
    {
        private readonly IRepository<Role, Guid> _roleRepository;
        private readonly IGuidGenerator _guidGenerator;

        public DefaultRoleDataSeeder(
            IRepository<Role, Guid> roleRepository,
            IGuidGenerator guidGenerator)
        {
            _roleRepository = roleRepository;
            _guidGenerator = guidGenerator;
        }

        /// <summary>
        /// 创建默认角色
        /// </summary>
        public async Task CreateDefaultRolesAsync()
        {
            // 管理员角色
            await CreateRoleIfNotExistsAsync("Admin", "管理员", "系统管理员，拥有所有权限", 
                isBuiltIn: true, priority: 1, permissions: new[]
                {
                    "Admin",
                    "Users.Manage",
                    "Agents.Manage",
                    "ModelConfigs.Manage",
                    "McpServers.Manage",
                    "Conversations.Manage",
                    "Statistics.View",
                    "System.Manage"
                });

            // 普通用户角色
            await CreateRoleIfNotExistsAsync("User", "用户", "普通用户，拥有基本权限", 
                isDefault: true, priority: 10, permissions: new[]
                {
                    "Agents.View",
                    "Agents.Create",
                    "ModelConfigs.View",
                    "McpServers.View",
                    "Conversations.View",
                    "Conversations.Create",
                    "Statistics.View"
                });

            // 开发者角色
            await CreateRoleIfNotExistsAsync("Developer", "开发者", "开发者，拥有开发和测试权限", 
                priority: 5, permissions: new[]
                {
                    "Agents.View",
                    "Agents.Create",
                    "Agents.Update",
                    "ModelConfigs.View",
                    "ModelConfigs.Create",
                    "ModelConfigs.Update",
                    "McpServers.View",
                    "McpServers.Create",
                    "McpServers.Update",
                    "Conversations.View",
                    "Conversations.Create",
                    "Statistics.View",
                    "System.View"
                });

            // 只读用户角色
            await CreateRoleIfNotExistsAsync("ReadOnly", "只读用户", "只读用户，只能查看数据", 
                priority: 20, permissions: new[]
                {
                    "Agents.View",
                    "ModelConfigs.View",
                    "McpServers.View",
                    "Conversations.View",
                    "Statistics.View"
                });
        }

        /// <summary>
        /// 创建角色（如果不存在）
        /// </summary>
        private async Task CreateRoleIfNotExistsAsync(
            string name,
            string displayName,
            string description,
            bool isBuiltIn = false,
            bool isDefault = false,
            int priority = 0,
            string[] permissions = null)
        {
            var existingRole = await _roleRepository.FirstOrDefaultAsync(r => r.Name == name);
            if (existingRole != null)
            {
                return;
            }

            var role = new Role(
                _guidGenerator.Create(),
                name,
                displayName,
                description,
                isBuiltIn,
                isDefault,
                priority);

            if (permissions != null)
            {
                role.SetPermissions(permissions);
            }

            await _roleRepository.InsertAsync(role);
        }
    }
}